from selenium.webdriver.common.by import By

from pages.base_page import BasePage
from selenium.webdriver.support.select import Select
class IndexPage(BasePage):
    #定位器
    #首页url
    url = "/crm/index.php?m=index&a=index"

    #右上角用户名定位
    head_photo_locator = (By.CSS_SELECTOR, "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul.nav.pull-right > li:nth-child(6) > a")
    #个人资料定位
    personal_datas_locator = (By.CSS_SELECTOR, "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul.nav.pull-right > li.dropdown.open > ul > li:nth-child(3) > a")
    #财务定位
    finance_locator = (By.CSS_SELECTOR, "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li:nth-child(8) > a")
    # 客户模块按钮定位
    customer_locator = (By.CSS_SELECTOR,
                        "body > div.navbar.navbar-inverse.navbar-fixed-top > div > div > div.nav-collapse.collapse > ul:nth-child(1) > li:nth-child(2) > a")
    # zyl销售机会模块定位器
    # 添加成功定位
    assert_locator = (By.CSS_SELECTOR, 'body > div.container > div.alert.alert-success')
    niche_locator = (By.LINK_TEXT, '商机')  # 商机
    add_niche_locator = (By.LINK_TEXT, '添加商机')  # 添加商机
    head_locator = (By.ID, 'owner_name')  # 负责人输入框
    input_customer_locator = (By.ID, 'customer_name')  # 客户输入框
    choice_three_locator = (
    By.CSS_SELECTOR, '#datas > tr:nth-child(2) > td:nth-child(1) > input[type=radio]:nth-child(1)')  # 选择客户(3位)
    choice_ten_locator = (
    By.CSS_SELECTOR, '#datas > tr:nth-child(2) > td:nth-child(1) > input[type=radio]:nth-child(1)')  # 选择客户(10位)
    confirm_locator = (By.XPATH, '/html/body/div[10]/div[3]/div/button[1]')  # 确认
    amount_locator = (By.ID, 'total_price')  # 商机金额输入框
    name_locator = (By.ID, 'name')  # 商机名输入框
    type_locator = (By.ID, 'type')  # 商机类型下拉框
    price_locator = (By.ID, 'estimate_price')  # 预计成交价输入框
    save_btn_locator = (By.CSS_SELECTOR, '#form1 > table > tfoot > tr > td > input:nth-child(1)')  # 保存

    # zyl办公模块定位器
    task_locator = (By.LINK_TEXT, '任务')  # 任务
    add_task_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > a')  # 添加任务
    theme_input_locator = (By.NAME, 'subject')  # 主题输入框
    head_input_locator = (By.ID, 'owner_name')  # 负责人输入框
    choice_head_locator = (By.CSS_SELECTOR, '#ta1 > span:nth-child(3) > input')  # 选择负责人
    ok_btn_locator = (By.CSS_SELECTOR,
                      'body > div:nth-child(12) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)')
    relevant_input_locator = (By.ID, 'about_roles_name')  # 任务相关人输入框
    mail_locator = (By.NAME, 'message_alert')  # 站内信
    email_locator = (By.NAME, 'email_alert')  # 邮箱
    relevant_ok_locator = (By.CSS_SELECTOR,
                           'body > div:nth-child(13) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1)')
    choice_select_locator = (By.ID, 'select1')  # 选择相关下拉框
    choice_input_locator = (By.ID, 'module_name')  # 选择相关输入框
    state_select_locator = (By.NAME, 'status')  # 状态下拉框
    data_input_locator = (By.ID, 'due_date')  # 截止时间输入框

    window_locator = (By.CSS_SELECTOR,
                      'body > div.container > div.row-fluid > div > form > table > tbody > tr:nth-child(7) > td:nth-child(2) > div > div.ke-edit > iframe')  # 描述框页面
    ms_input_locator = (By.CSS_SELECTOR, 'body')  # 描述输入框
    save_locator = (By.CSS_SELECTOR,
                        'body > div.container > div.row-fluid > div > form > table > tfoot > tr > td:nth-child(2) > input:nth-child(1)')  # 保存按钮
    build_locator = (By.CSS_SELECTOR,'body > div.container > div.row-fluid > div > form > table > tfoot > tr > td:nth-child(2) > input:nth-child(2)') # 保存并新建

    alert_locator = (By.CSS_SELECTOR,'body > div.container > div.row-fluid > div > div') # 添加成功返回数据


    #点击财务按钮
    def finance_click(self):
        self.find_element(self.finance_locator).click()

    #点击个人资料按钮
    def personal_datas_click(self):
        self.find_element(self.personal_datas_locator).click()

    #点击右上角用户名头像按钮
    def head_photo_click(self):
        self.find_element(self.head_photo_locator).click()

    # 站内信图标定位器
    letter_btn_locator = (By.CSS_SELECTOR, "#message_tips > i")
    def click_letter(self):
        self.find_element(self.letter_btn_locator).click()

    # 查看站内信提醒
    letter_locator = (By.CSS_SELECTOR, "#message_item > span.tips_link > a")
    def look_letter(self):
        self.find_element(self.letter_locator).click()
    #断言站内信提醒
    def look_letter_text(self):
        return self.find_element(self.letter_locator).text

    # zyl销售机会模块方法
    # 添加商机
    def add_niche(self):
        self.driver.find_element(*self.niche_locator).click()  # 点击商机
        self.driver.find_element(*self.add_niche_locator).click()  # 点击添加商机
    # 输入负责人
    def input_head(self,head):
        self.driver.find_element(*self.head_locator).send_keys(head)  # 在负责人输入框输入信息
    # 选择客户(3位)
    def choice_three_customer(self):
        self.driver.find_element(*self.input_customer_locator).click()
        self.driver.find_element(*self.choice_three_locator).click()  # 选择客户
        self.driver.find_element(*self.confirm_locator).click()  # 点击确认
    # 选择客户(10位)
    def choice_ten_customer(self):
        self.driver.find_element(*self.input_customer_locator).click()
        self.driver.find_element(*self.choice_ten_locator).click()  # 选择客户
        self.driver.find_element(*self.confirm_locator).click()  # 点击确认
    # 输入商机金额
    def input_sj_price(self,price):
        self.driver.find_element(*self.amount_locator).send_keys(price)  # 在商机金额输入框输入信息
    # 输入商机名
    def input_sj_name(self,name):
        self.driver.find_element(*self.name_locator).send_keys(name)  # 在商机名输入框输入信息
    # 选择商机类型
    def sj_type(self,value):
        self.driver.find_element(*self.type_locator).click()  # 点击商机类型
        type_element = self.driver.find_element(*self.type_locator)  # 返回商机类型的元素
        type_select = Select(type_element)  # 实例化
        type_select.select_by_visible_text(value)  # 选择商机类型(第一个值)
    # 输入预计成交价
    def input_yj_price(self,price):
        self.driver.find_element(*self.price_locator).send_keys(price)  # 在预计成交价输入框输入信息
    # 保存
    def save_sj(self):
        self.driver.find_element(*self.save_btn_locator).click()  # 点击保存
    # 获取添加成功文本信息
    def get_assert_text(self):
        actul = self.driver.find_element(*self.assert_locator).text
        return actul

    # zyl办公模块方法
    # 添加任务
    def add_task(self):
        self.driver.find_element(*self.task_locator).click()  # 点击任务
        self.driver.find_element(*self.add_task_locator).click()  # 点击添加任务按钮
    # 主题输入框输入信息
    def input_theme(self,theme):
        self.driver.find_element(*self.theme_input_locator).send_keys(theme)  # 主题输入框输入信息
    # 选择负责人
    def choice_head(self):
        self.driver.find_element(*self.head_input_locator).click()  # 点击负责人输入框
        self.driver.find_element(*self.choice_head_locator).click()  # 选择负责人
        self.driver.find_element(*self.ok_btn_locator).click()  # 点击ok键
    # 选择任务相关人
    def choice_relevant(self):
        self.driver.find_element(*self.relevant_input_locator).click()  # 点击任务相关人输入框
        self.driver.find_element(*self.choice_head_locator).click()  # 选择任务相关人
        self.driver.find_element(*self.relevant_ok_locator).click()  # 点击ok键
    # 选择相关下拉框
    def relevant_select(self,relevant):
        self.driver.find_element(*self.choice_select_locator).click()  # 点击选择相关下拉框
        choice_element = self.driver.find_element(*self.choice_select_locator)  # 返回下拉框元素
        choice_select = Select(choice_element)  # 实例化select类
        choice_select.select_by_visible_text(relevant)  # 选择联系人
    # 取消站内信
    def cancel_mail(self):
        self.driver.find_element(*self.mail_locator).click()
    # 点击邮件
    def choice_email(self):
        self.driver.find_element(*self.email_locator).click()
    # 选择相关输入框输入信息
    def input_relevant(self,relevant):
        self.driver.find_element(*self.choice_input_locator).send_keys(relevant)  # 选项相关输入框输入信息
    # 状态下拉框选择数据
    def state_select(self,state):
        self.driver.find_element(*self.state_select_locator).click()  # 点击状态下拉框
        state_element = self.driver.find_element(*self.state_select_locator)  # 返回下拉框元素
        state_select = Select(state_element)  # 实例化select类
        state_select.select_by_visible_text(state)  # 选择进行中
    # 截止时间输入框输入信息
    def input_data(self, data):
        self.driver.find_element(*self.data_input_locator).send_keys(data)  # 截止时间输入框输入信息
    # 描述框输入内容
    def input_ms(self,ms):
        window_element = self.driver.find_element(*self.window_locator)  # 取到描述框元素
        self.driver.switch_to.frame(window_element)  # 切换到描述框
        self.driver.find_element(*self.ms_input_locator).send_keys(ms) # 输入信息
        self.driver.switch_to.parent_frame()  # 切回上一界面
    # 保存
    def save_btn(self):
        self.driver.find_element(*self.save_locator).click()  # 点击保存
    # 保存并新建
    def build_btn(self):
        self.driver.find_element(*self.build_locator).click()  # 点击保存并新建
    def get_alert_text(self):
        actul = self.driver.find_element(*self.alert_locator).text
        return actul
